package air_cp5620.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;


public class DAO {
    private Connection connection = null;
    private final String JNDINAME = "com.mysql.jdbc.Driver";
    private String URL = "jdbc:mysql://localhost:3306/air";
    private String dbName = "root";
    private String dbPaw = "123456";

    public DAO()
    {
        init();
    }

    private void init () {
        try {
            Class.forName(JNDINAME);
            connection = DriverManager.getConnection(URL,dbName , dbPaw);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Database connection failed");
            e.printStackTrace();
            connection = null;
        }
    }


    public Connection getConnection() {
        if (connection == null) {
            init();
        }
        return connection;
    }


    public void setConnection (String url,String name,String paw) {
        this.URL = url;
        this.dbName = name;
        this.dbPaw = paw;
        connection = null;
        init();
    }

}
